<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { guild_roomApi } from '@/api/controller';
  import { getDateArray } from '@/utils';
  import { ToastSuccess, ToastWarning, Toastloading,  } from '@/utils/public';
  import { ElMessageBox } from 'element-plus';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogTableVisible:false,
    dialogFormVisible:false,
    disabled:false,
    title:'',
    list:[] as Array<any>,
    dateArr:getDateArray('ymdhis') as Array<number>
  })
  interface formQuert {
    content:string
    wg_time:string
  }
  const listQuery = reactive({
    begin_date:'',
    end_date:'',
    room_id:''
  })
  let form = reactive({} as formQuert)
  const formRules = {
    content: [{ required: true, trigger: 'blur', message: '请输入违规内容' }],
    wg_time: [{ required: true, trigger: 'blur', message: '请选择违规时间' }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading();
      guild_roomApi.addCbdViolationLog({
        room_id:listQuery.room_id,
        ...form
      }).then(()=>{
        ToastSuccess("操作成功!");
        getList();
        loading.close();
      }).catch(()=>{
        loading.close();
      })
    })
  }
  const handleAddLog = (row:any = {})=>{
    const { id=0, content='', wg_time='' } = row
    let obj:any = {
      room_id:listQuery.room_id,
      wg_time,
      content
    }
    if(id) {
      obj['id'] = id
    }
    Object.assign(form,obj)
    action.dialogFormVisible = true
  }
  const handleDelete = (row:any = {})=>{
    const { id=0 } = row
    if(!id) return ToastWarning('log id error')
    ElMessageBox.confirm("确定删除此条违规记录吗?", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(async () => {
      const loading = Toastloading();
      guild_roomApi.delviolationRecord(row.id).then(()=>{
        ToastSuccess("操作成功!");
        loading.close();
        getList();
      }).catch(()=>{
        loading.close();
      })
    }).catch(() => {});
  }
  const getList = async()=>{
    const loading = Toastloading()
    const res = await guild_roomApi.getViolationRecordList(listQuery.room_id, listQuery)
    const { data=[] } = res
    action.list = data
    loading.close()
  }
  const openDialog = async(query:any = {})=> {
    const { name='', room_id=0 } = query
    action.title = `${name}-违规记录`
    listQuery.room_id = room_id
    await getList()
    action.dialogTableVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="5vh"
    v-model="action.dialogTableVisible"
    class="xm-dialog"
    width="50%"
    :title="action.title">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">日期：</div>
          <el-date-picker
            v-model="action.dateArr"
            type="datetimerange"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="handleAddLog()">新 增</el-button>
        </div>
      </div>
      <el-table class="xm-TableCenter" :data="action.list" border>
        <el-table-column min-width="100" prop="content" label="违规内容" />
        <el-table-column min-width="100" prop="wg_time" label="违规时间" />
        <el-table-column min-width="100" v-rule="'room_closeRoom'" label="操作">
            <template #default="scope">
              <el-button
                type="primary"
                @click="handleAddLog(scope.row)"
              >编 辑</el-button>
              <el-button
                type="danger"
                @click="handleDelete(scope.row)"
              >删 除</el-button>
            </template>
          </el-table-column>
      </el-table>
    </el-dialog>
    <el-dialog
    top="5vh"
    class="xm-dialog input-width"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="action.disabled"
    :show-close="action.disabled"
    :close-on-press-escape="action.disabled"
    width="50%"
    :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="违规内容" prop="content">
          <el-input
            v-model="form.content"
            :autosize="{ minRows: 6, maxRows: 6}"
            type="textarea"
            placeholder="请输入违规内容"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="违规时间" prop="wg_time">
          <el-date-picker
            v-model="form.wg_time"
            type="datetime"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="选择日期时间" />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<style lang="scss" scoped>
  .labelBox {
    width: 100%;
  }
</style>